package com.longxi.tianjintianbao.ui.presenter.quality;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.SeekBar;

import com.longxi.tianjintianbao.AppConst;
import com.longxi.tianjintianbao.MyApplication;
import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.api.ApiRetrofit;
import com.longxi.tianjintianbao.api.response.BaseResponse;
import com.longxi.tianjintianbao.exception.ExceptionHandle;
import com.longxi.tianjintianbao.exception.MySubscriber;
import com.longxi.tianjintianbao.model.quality.QualityItem;
import com.longxi.tianjintianbao.ui.base.BaseActivity;
import com.longxi.tianjintianbao.ui.base.BasePresenter;
import com.longxi.tianjintianbao.ui.view.quality.IQualityScoreContentAtView;
import com.longxi.tianjintianbao.utils.MyUtils;
import com.longxi.tianjintianbao.utils.TypeTransformUtils;

import org.json.JSONException;
import org.json.JSONObject;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class QualityScoreAtPresenter extends BasePresenter<IQualityScoreContentAtView> {

    QualityItem qualityItem;
    public String rectification = "否";

    public QualityScoreAtPresenter(BaseActivity context) {
        super(context);
    }

    public void setBar() {
        Intent intent = mContext.getIntent();
        qualityItem = (QualityItem) intent.getSerializableExtra(AppConst.QUALITYITEM);
        setQualityItem();
        mContext.setTitle("打分");
        mContext.setTvLeft(MyUtils.getString(R.string.back));
    }

    private void setQualityItem(){
        StringBuilder buffer = new StringBuilder();

        if(!TextUtils.isEmpty(qualityItem.getItemname())){
            buffer.append("项名称:");
            buffer.append(qualityItem.getItemname());
            buffer.append("\n");
        }

        if(!TextUtils.isEmpty(qualityItem.getTypename())){
            buffer.append("类别名称:");
            buffer.append(qualityItem.getTypename());
            buffer.append("\n");
        }

        if(!TextUtils.isEmpty(qualityItem.getItemstandard())){
            buffer.append("项目标准:");
            buffer.append(qualityItem.getItemstandard());
            buffer.append("\n");
        }

        if(!TextUtils.isEmpty(qualityItem.getAllscore())){
            buffer.append("总分:");
            buffer.append(qualityItem.getAllscore());
        }

        getView().getTvItemName().setText(buffer);

        getView().getEtScore().setText(qualityItem.getScore());
        getView().getEtReason().setText(qualityItem.getReason());

        rectification = qualityItem.getModifystate();
        if("是".equals(rectification)){
            getView().getRadioRectification().check(R.id.rBtnYes);
        }else{
            getView().getRadioRectification().check(R.id.rBtnNo);
        }
    }

    /**
     * 品质检查打分
     */
    public void saveQualityScore() {
        String score = getView().getEtScore().getText().toString().trim();
        String reason = getView().getEtReason().getText().toString().trim();
        if(TextUtils.isEmpty(score)){
            MyUtils.showToast("请输入分数!");
            return;
        }

        JSONObject object = new JSONObject();
        try {
            object.put("userid", MyApplication.loginUser.userid);
            object.put("itemid", qualityItem.getItemid());
            object.put("score", score);
            object.put("reason", reason);
            object.put("rectification", rectification);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ApiRetrofit.getInstance().saveQualityScore(object)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new MySubscriber<BaseResponse>(mContext) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable throwable) {
                mContext.hideWaitingDialog();
                MyUtils.showToast(throwable.message);
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                if(baseResponse.getCode() != 0){
                    MyUtils.showToast(baseResponse.getMsg());
                    return;
                }
                MyUtils.showToast("保存成功");
                mContext.setResult(mContext.RESULT_OK);
                mContext.finish();
            }
        });
    }

}
